package code.jam.context;

import my.andoird.util.r1.AndroidString;

public class JamChord
{
	public static int searchMinorType(StringBuilder string)
	{
		int ret = 0;

		String src = string.toString();

		if (src == "natural")
		{
			ret = JamGlossaryContext.SCALE_MINOR_NATURAL;
		}
		else if (src == "harmonic")
		{
			ret = JamGlossaryContext.SCALE_MINOR_HARMONIC;
		}
		else
		{
			ret = JamGlossaryContext.SCALE_MINOR_MELODIC;
		}

		return ret;
	}

	public static int[] searchScale(String source)
	{
		int scale[] = new int[3];

		StringBuilder string = new StringBuilder();

		int i = 0;

		for (; i < source.length(); i++)
		{
			if (source.charAt(i) == '.')
				break;

			string.append(source.charAt(i));

			scale[0] = searchKey(string);
		}

		AndroidString.clear(string);
		for (i++; i < source.length(); i++)
		{
			if (source.charAt(i) == '.')
				break;

			string.append(source.charAt(i));

			scale[1] = searchMajorType(string);
		}

		AndroidString.clear(string);
		for (i++; i < source.length(); i++)
		{
			string.append(source.charAt(i));

			scale[2] = searchMinorType(string);
		}

		return scale;
	}

	public static int searchMajorType(StringBuilder string)
	{
		int ret = 0;
		String src = string.toString();

		if (src == "major")
			ret = JamGlossaryContext.SCALE_MAJOR;
		else
			ret = JamGlossaryContext.SCALE_MINOR_NATURAL;

		return ret;
	}

	public static int searchKey(StringBuilder string)
	{
		int ret = 0;

		String src = string.toString();

		src.trim();

		if (src.compareTo("A") == 0)
		{
			ret = JamGlossaryContext.KEY_A;
		}
		else if (src.compareTo("A#") == 0)
		{
			ret = JamGlossaryContext.KEY_A_SHARP;
		}
		else if (src.compareTo("B") == 0)
		{
			ret = JamGlossaryContext.KEY_B;
		}
		else if (src.compareTo("C") == 0)
		{
			ret = JamGlossaryContext.KEY_C;
		}
		else if (src.compareTo("C#") == 0)
		{
			ret = JamGlossaryContext.KEY_C_SHARP;
		}
		else if (src.compareTo("D") == 0)
		{
			ret = JamGlossaryContext.KEY_D;
		}
		else if (src.compareTo("D#") == 0)
		{
			ret = JamGlossaryContext.KEY_D_SHARP;
		}
		else if (src.compareTo("E") == 0)
		{
			ret = JamGlossaryContext.KEY_E;
		}
		else if (src.compareTo("F") == 0)
		{
			ret = JamGlossaryContext.KEY_F;
		}
		else if (src.compareTo("F#") == 0)
		{
			ret = JamGlossaryContext.KEY_F_SHARP;
		}
		else if (src.compareTo("G") == 0)
		{
			ret = JamGlossaryContext.KEY_G;
		}
		else if (src.compareTo("G#") == 0)
		{
			ret = JamGlossaryContext.KEY_G_SHARP;
		}

		// Log.e( DEBUG_TAG, src + "=" +String.valueOf( ret ) );

		return ret;
	}

	public static int[] searchChordProgression(String source)
	{
		int[] array = new int[2];

		StringBuilder string = new StringBuilder();

		int i = 0;

		for (; i < source.length(); i++)
		{
			if (source.charAt(i) == '.')
				break;

			string.append(source.charAt(i));

			array[0] = searchKey(string);
		}

		if (i == source.length())
		{
			array[1] = JamChord.searchChordType(null);
			return array;
		}

		AndroidString.clear(string);

		for (i++; i < source.length(); i++)
		{
			string.append(source.charAt(i));

			array[1] = JamChord.searchChordType(string);
		}

		return array;

	}

	public static int searchChordType(StringBuilder string)
	{
		int ret = 0;

		if (string == null)
		{
			ret = JamGlossaryContext.R_Major;
		}
		else
		{
			String src = string.toString();

			if (src.compareTo("m") == 0)
			{
				ret = JamGlossaryContext.R_Minor;
			}
			else if (src.compareTo("s5") == 0)
			{
				ret = JamGlossaryContext.R_s5;
			}
			else if (src.compareTo("6") == 0)
			{
				ret = JamGlossaryContext.R_6;
			}
			else if (src.compareTo("7") == 0)
			{
				ret = JamGlossaryContext.R_7;
			}
			else if (src.compareTo("9") == 0)
			{
				ret = JamGlossaryContext.R_9;
			}
			else if (src.compareTo("M7") == 0)
			{
				ret = JamGlossaryContext.R_Major7;
			}
			else if (src.compareTo("m6") == 0)
			{
				ret = JamGlossaryContext.R_Minor6;
			}
			else if (src.compareTo("m7") == 0)
			{
				ret = JamGlossaryContext.R_Minor7;
			}
			else if (src.compareTo("mM7") == 0)
			{
				ret = JamGlossaryContext.R_MinorMajor7;
			}
			else if (src.compareTo("m7b5") == 0)
			{
				ret = JamGlossaryContext.R_Minor7b5;
			}
			else if (src.compareTo("dim") == 0)
			{
				ret = JamGlossaryContext.R_dim;
			}
			else if (src.compareTo("dim7") == 0)
			{
				ret = JamGlossaryContext.R_dim7;
			}
			else if (src.compareTo("sus4") == 0)
			{
				ret = JamGlossaryContext.R_sus4;
			}
		}

		return ret;
	}
}
